package org.wmq.positiontoshop.model.ES;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import org.springframework.data.elasticsearch.annotations.GeoPointField;
import org.springframework.data.elasticsearch.core.geo.GeoPoint;

import java.time.LocalDateTime;

/**
 * Elasticsearch店铺实体类
 * 用于在Elasticsearch中存储和查询店铺信息
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Document(indexName = "shop-index")
public class ShopES {
    @Id
    private Long id;
    
    @Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_smart")
    private String name;
    
    @Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_smart")
    private String description;
    
    @GeoPointField
    private GeoPoint location;
    
    @Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_smart")
    private String address;
    
    @Field(type = FieldType.Keyword)
    private String phone;
    
    @Field(type = FieldType.Text)
    private String businessHours;
    
    @Field(type = FieldType.Keyword)
    private String type;
    
    @Field(type = FieldType.Double)
    private Double rating;
    
    @Field(type = FieldType.Integer)
    private Integer status;
    
    @Field(type = FieldType.Date)
    private LocalDateTime createTime;
    
    @Field(type = FieldType.Date)
    private LocalDateTime updateTime;
    
    /**
     * 将DO对象转换为ES对象
     */
    public static ShopES fromDO(org.wmq.positiontoshop.model.DO.Shop shop) {
        ShopES shopES = new ShopES();
        shopES.setId(shop.getId());
        shopES.setName(shop.getName());
        shopES.setDescription(shop.getDescription());
        
        // 设置地理位置
        if (shop.getLatitude() != null && shop.getLongitude() != null) {
            shopES.setLocation(new GeoPoint(shop.getLatitude(), shop.getLongitude()));
        }
        
        shopES.setAddress(shop.getAddress());
        shopES.setPhone(shop.getPhone());
        shopES.setBusinessHours(shop.getBusinessHours());
        shopES.setType(shop.getType());
        shopES.setRating(shop.getRating());
        shopES.setStatus(shop.getStatus());
        shopES.setCreateTime(shop.getCreateTime());
        shopES.setUpdateTime(shop.getUpdateTime());
        
        return shopES;
    }
}